<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------
define('LG_ADMIN_LOGIN', true);

require __DIR__ . '/../autoload.php';

use Lg\View\View;
use Lg\Support\Redirect;
use Lg\Http\Input;

//use Lg\Http\Cookie;
// +----------------------------------------------------------------------
// | user login
// +----------------------------------------------------------------------
function indexAction() {
    if (Input::isMethod('post')) {
        if (LoginValidator::fails()) {
            return Redirect::to('/auth/login.php')
                            ->withFlash('error', __('请输入正确的用户名和密码'));
        }
        if (user_login(Input::post('username'), Input::post('password'))) {
            //login
            $resp = Redirect::to('/dashboard/')
                    ->maybe_redirect();
//            if (Input::post('rememberme', false) == true) {
//                $resp->withCookie(Cookie::make('rememberadm', '', time() + 86400 * 7));
//            }       
            return $resp->withFlash('success',
                            sprintf('您好，欢迎登陆%s控制面板!,上次使用%s IP地址访问, 登陆时间 %s', get_option('system.sitename', 'H1CMS'), Input::req()->ip(),
                                    date('Y-m-d H:i:s', \h1cms\system\Auth::user()->lasttime)
            ));
        }
        return Redirect::to('/auth/login.php')
                        ->withFlash('error', __('用户名或密码不正确'));
    }
    return View::make('login');
}
